﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace PPTHelper
{
    public class PPTTools
    {
        /// <summary>
        /// 对裁剪过的图片进行更改图片
        /// </summary>
        /// <param name="selSlide">当前幻灯页</param>
        /// <param name="oldShape">需要替换的图片的形状</param>
        /// <returns> 替换后的图片形状</returns>
        public static PowerPoint.Shape PictureChange(PowerPoint.SlideRange selSlide, PowerPoint.Shape oldShape)
        {
            PowerPoint.Shapes slideShapes = selSlide.Shapes; // 当前幻灯片的Shapes
            PowerPoint.PictureFormat pic = oldShape.PictureFormat;

            // 选择要替换成的图片
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.Filter = "图像文件(*.jpg;*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png";
            fileDialog.Title = "请选这一张图片";

            if (fileDialog.ShowDialog() != DialogResult.OK) return null; // 如果选择文件失败就退出
            string newPictureFilePath = fileDialog.FileName;

            // 插入一张图片
            PowerPoint.Shape newShape = slideShapes.AddPicture2(newPictureFilePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, oldShape.Left, oldShape.Top, pic.Crop.PictureWidth, pic.Crop.PictureHeight, Office.MsoPictureCompress.msoPictureCompressFalse);

            // 复制属性
            oldShape.PickUp();
            newShape.Apply();

            // 设置新插入的图片的裁剪
            newShape.PictureFormat.Crop.PictureHeight = pic.Crop.PictureHeight;
            newShape.PictureFormat.Crop.PictureWidth = pic.Crop.PictureWidth; // 图片的原始尺寸

            newShape.PictureFormat.Crop.ShapeHeight = pic.Crop.ShapeHeight; 
            newShape.PictureFormat.Crop.ShapeWidth = pic.Crop.ShapeWidth;  // 裁剪后的形状的尺寸
            newShape.PictureFormat.Crop.ShapeTop = pic.Crop.ShapeTop;
            newShape.PictureFormat.Crop.ShapeLeft = pic.Crop.ShapeLeft;  // 裁剪后的形状的位置

            // 要先设置尺寸和裁剪
            newShape.PictureFormat.Crop.PictureOffsetX = pic.Crop.PictureOffsetX;
            newShape.PictureFormat.Crop.PictureOffsetY = pic.Crop.PictureOffsetY; // 图片相对于裁剪形状的位置

            int zOrderPosition = oldShape.ZOrderPosition;
            oldShape.Delete();

            newShape.ZOrder(Office.MsoZOrderCmd.msoSendToBack);
            for (int i = 0; i < zOrderPosition-1; i++)
            {
                newShape.ZOrder(Office.MsoZOrderCmd.msoBringForward);
            }

            return newShape;
        }

        /// <summary>
        /// 对一个组里的裁剪过的图片进行更改
        /// </summary>
        /// <param name="selSlide">当前幻灯页</param>
        /// <param name="oldShape">原始图片</param>
        /// <param name="groupShape">图片所在的组</param>
        public static void PictureChange(PowerPoint.SlideRange selSlide, PowerPoint.Shape oldShape, PowerPoint.ShapeRange groupShape)
        {
            PowerPoint.GroupShapes groupItems = groupShape.GroupItems;
            PowerPoint.ShapeRange itemShapes =  groupShape.Ungroup(); // ungroup会改变ZOrderPosition所以要先解组在进行替换

            PowerPoint.Shape newShape = PPTTools.PictureChange(selSlide, oldShape);

            if (newShape != null)
            {

                string[] name = new string[groupItems.Count + 1];

                for (int i = 1; i <= groupItems.Count; i++)
                {
                    name[i - 1] = groupItems[i].Name; // groupItems的索引从1开始
                }

                name[groupItems.Count] = newShape.Name;

                if (name.Count() != 0)
                {

                    var newGroup = selSlide.Shapes.Range(name).Group();
                }
            }
            else
            {
                itemShapes.Group();
            }
        }
    }
}
